package org.vectrics.skill.struts.pagelist;

import java.util.Iterator;

import org.apache.log4j.Logger;
import org.vectrics.common.tree.TreeHelper;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.skill.Skill;
import org.vectrics.skill.SkillRating;
import org.vectrics.skill.SkillRatingSet;
import org.vectrics.skill.struts.SkillWebUtils;
import org.vectrics.struts.Request;
import org.vectrics.user.Person;
import org.vectrics.user.UserSkillRatingSet;

public class PageListSkillSessionHelper {
	private Logger log = Logger.getLogger(this.getClass());
	
	public void saveSkillsSelectionsToSessionData(Request request, Person person) {
		SkillRatingSet skillRatingSet = this.getSessionSkillRatingSet(request);
		if (skillRatingSet == null) {
			skillRatingSet = new UserSkillRatingSet(person);
			SkillWebUtils.setSkillRatingSet(request, skillRatingSet);
		}
		this.saveSkillsSelectionsToRatingList(request, skillRatingSet);
    	SkillWebUtils.setSkillRatingSet(request, skillRatingSet);
	}
	
	public UserSkillRatingSet getSessionSkillRatingSet(Request request) {
		UserSkillRatingSet skillRatingSet = (UserSkillRatingSet)SkillWebUtils.getSkillRatingSet(request);
		return(skillRatingSet);
	}
	
	public void saveSkillsSelectionsToRatingList(Request request, SkillRatingSet skillRatingSet) {
		Skill rootSkill = RecruitServiceLocator.getSkillProvider().getRootSkill();
		this.readFormSkillEntries(request, rootSkill, skillRatingSet);
	}
	
    protected void readFormSkillEntries(Request request, Skill rootSkill, SkillRatingSet skillRatingSet) {
	    Iterator<String> paramEnum = request.getParameterNames().iterator();
    	SkillRating rating = null;
	    
	    while (paramEnum.hasNext()) {
	        String paramName = (String)paramEnum.next();
	        String value = request.getParameter(paramName);
	        log.debug("doPerform() - looking at param -  name : " + paramName);                        
	        log.debug("doPerform() - looking at param - value : " + value);                        
	
	        
	        if (paramName.startsWith("yrs_")) {
	            log.debug("doPerform() - yrs_ param found.  Setting skill years.");
	            if  (value.trim().length() > 0) {
	                        
	                int valueInt = 0;
	                try {
	                    valueInt = Integer.parseInt(value);
	                } catch (Exception ex) {
	                    log.error("Exception ", ex);
	                    valueInt = -1;
	                }
	                        
	                if (valueInt > 0) {
	                    log.debug("doPerform() - yrs rating found: " + paramName);
	                    String skillCode = paramName.substring(4);
	                    Skill skill = (Skill)TreeHelper.findNode(rootSkill, skillCode);
	                    
	                    if (skill != null) {
	                        rating = skillRatingSet.findSkillRating(skill);
	                        if (rating == null) {
	                            log.debug("doPerform() - creating new skill rating.");
	                            rating = skillRatingSet.addRating(skill);
	                            rating.setLevel("0");
	                        }
	                        log.debug("Setting yrs to " + value);
	                        rating.setLevel(value);
	                    }
	                    else {
	                        log.error("doPerform() - Could not find skill for param: "
	                                + paramName + "  Code = " + skillCode);
	                    };
	                }
	            }
	            else {
	                log.debug("doPerform() - yrs_ param had blank value - ignored");
	            }
	        } else if (paramName.startsWith("lvl_")) {
	            if (value.trim().length() > 0) { 
	                log.debug("doPerfom() - 'lvl-*** - rating level found.");
	                String skillCode = paramName.substring(4);
	                Skill skill = (Skill)TreeHelper.findNode(rootSkill, skillCode);
	                if (skill != null) {
	                    rating = skillRatingSet.findSkillRating(skill);
	                    int ratingLevel = Integer.parseInt(value);
	                    if ((rating == null) && (ratingLevel > 0)) {
	                        log.debug("doPerform() - found lvl - creating new skill rating.");
	                        
	                        rating = skillRatingSet.addRating(skill);
	
	                        if (ratingLevel <= 0)
	                            rating.setLevel("0");
	                        else 
	                            rating.setLevel("0");
	                        rating.setSkill(skill);
	                    }
	                    
	                    if (rating != null) {
	                        log.debug("Setting lvl to " + value);
	                        rating.setLevel(value);
	                    }
	                    
	                }
	                else {
	                    log.error("doPerform() - Could not find skill for param: "
	                            + paramName + "  Code = " + skillCode);
	                };
	            }
	            else {
	                log.debug("doPerform() - lvl_ param had blank value.  Ignored.");
	            }
	        }
	        else if (paramName.startsWith("yn_")) {
	            String skillCode = paramName.substring(3);
	            log.debug("doPerform() - checked: " + skillCode);
	            Skill skill = (Skill)TreeHelper.findNode(rootSkill, skillCode);
	            
	            if (skill != null) {
	                rating = skillRatingSet.findSkillRating(skill);
	                if (rating == null) {
	                	log.debug("did not find existing rating, creating new one.");
	                    rating = skillRatingSet.addRating(skill);
	                    rating.setLevel("y");
	                } else {
	                	rating.setLevel("y");
	                }
	            }
	            else {
	                log.error("doPerform() - Could not find skill for param: "
	                        + paramName + "  Code = " + skillCode);
	            };
	        }
	        else if (paramName.startsWith("children_")) {
	            String skillCode = paramName.substring(9);
	            log.debug("doPerform() - view children checked, skill = " + skillCode);
	            Skill skill = (Skill)TreeHelper.findNode(rootSkill, skillCode);
	            
	            if (skill != null) {
	            	rating = skillRatingSet.findSkillRating(skill);
	                if (rating == null) {
	                    rating = skillRatingSet.addRating(skill);
	                    rating.setLevel(null);
	                    rating.setSkill(skill);
	                }
	                
	                rating.setViewChildren(new Boolean(true));
	            }
	            else {
	                log.error("doPerform() - Could not find skill for param: "
	                        + paramName + "  Code = " + skillCode);
	            };
	        }
	        else {
	            log.debug("doPerform() - param did not match a pattern: " + paramName);
	        }
	    }
	    if ((rating != null) && (rating.getScore() == 0)) {
	    	skillRatingSet.removeSkillRating(rating.getSkill());
	    }
    }

}
